package com.hsnn.medstgmini.common.interceptor;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class StdAttrInterceptor implements HandlerInterceptor {
	
	@Value("${file.suffix}")
	private String fileSuffix;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//  这儿处理
		boolean bool = true;
		String suffix = request.getParameter("suffix");
		if(StringUtils.isEmpty(suffix) || !fileSuffix.contains(suffix)){
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			OutputStream os = response.getOutputStream();
			JSONObject returnMap = new JSONObject();// 封装返回信息
			returnMap.put("msg", "格式不对");
			os.write(JSON.toJSONString(returnMap).getBytes("UTF-8"));
			os.flush();
			os.close();
			bool = false;
		}
		return bool;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
